// pages/loginpublic/loginpublic.js
import http from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   show:false,showmodal:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      nick:options.nick,
      img:options.img
    })
       wx.login({
        success:(res)=>{
          let code=res.code
          http('/login/getopenid','post',{ js_code: res.code}).then(res=>{
           console.log(res);
         this.setData({
           openid:res.data.data.openid,
           session_key:res.data.data.session_key
         })
        })}})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getPhoneNumber (e) {
        console.log(e);
        let authorData = e.detail;
        if (authorData.errMsg == 'getPhoneNumber:ok') {
              wx.showLoading({
           title: '正在登录...',
         }) 
             var signature = authorData.signature
             var rawData = authorData.rawData
             var encryptedData = authorData.encryptedData
             var iv = authorData.iv
             let openid=this.data.openid
             let session_key=this.data.session_key
             wx.setStorageSync('openId', openid)
             wx.setStorageSync('session_key', session_key)
             //请求获取用户手机号信息并进行校验后登录
             var reqData = {
               sessionKey: session_key,
               encryptedData: encryptedData,
               iv: iv
             };
               http('/login/demo','post',reqData).then(res=>{
               console.log(res);
               let res1=JSON.parse(res.data.data)
               console.log(res1.phoneNumber);
               console.log("pid是",wx.getStorageSync('pid'));
               // getApp().globalData.islogin = true
               let data={Openid:openid,pid:wx.getStorageSync('pid'),phone:res1.phoneNumber,
              img:this.data.img,nick:this.data.nick}
               http('/login/login','post',data).then(res=>{
                 console.log(res);
                 wx.setStorageSync('uid', res.data.data)
                 wx.setStorageSync('islogin', true)
                 wx.navigateBack({
                   delta: 0,
                 })
                 wx.hideLoading({
                   success: (res) => {},
                 })
               }).catch(res=>{
                 wx.showToast({
                   title: '登陆失败',
                 })
               })
             }).catch(res=>{
              console.log(res);
             })
            this.setData({
               data:res.data.data
             })
   }
  },
  login(){
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (file) => {console.log(file);
        this.setData({
          nick:file.userInfo.nickName,
          img:file.userInfo.avatarUrl,
          showmodal:true
        })
        // wx.navigateTo({
        //   url: '/pages/loginpublic/loginpublic?nick='+file.userInfo.nickName+'&img='+file.userInfo.avatarUrl,
        // })
      }})
  }

})